/**
 * 
 */
package dao;

import util.DAL;
import util.MyConnectUnit;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import model.BacDaoTaoM;

public class BacDaoTaoD {
	private MyConnectUnit conn;

	/**
	 * @function constructor BacDaoTaoD
	 * @throws Exception
	 */
	public BacDaoTaoD() throws Exception {
		super();
		this.conn = DAL.getDAL();
	}

	/**
	 * @function get all BacDaoTao
	 * @param Condition
	 * @param Limit
	 * @param OrderBy
	 * @return
	 * @throws Exception
	 */
	public ArrayList<BacDaoTaoM> getAll(String Condition, String Limit, String OrderBy)
			throws Exception {
		ArrayList<BacDaoTaoM> ls = new ArrayList<BacDaoTaoM>();
		ResultSet rs = this.conn.Select("BAC_DAO_TAO", Condition, Limit, OrderBy);
		BacDaoTaoM l = null;
		while (rs.next()) {
			l = new BacDaoTaoM();
			l.setId_bac_dao_tao(rs.getInt("id_bac_dao_tao"));
			l.setTen(rs.getString("ten"));
			ls.add(l);
		}
		return ls;
	}

	/**
	 * @function get all BacDaoTao with Condition + Limit
	 * @param Condition
	 * @param Limit
	 * @return
	 * @throws Exception
	 */
	public ArrayList<BacDaoTaoM> getAll(String Condition, String Limit) throws Exception {
		return getAll(Condition, Limit, null);
	}

	/**
	 * @function get all BacDaoTao with Condition
	 * @return
	 * @throws Exception
	 */
	public ArrayList<BacDaoTaoM> getAll(String Condition) throws Exception {
		return getAll(Condition, null);
	}
	
	/**
	 * @function get all BacDaoTao no parameter
	 * @return
	 * @throws Exception
	 */
	public ArrayList<BacDaoTaoM> getAll() throws Exception {
		return getAll(null);
	}
	
	/**
	 * @function search ten BacDaoTao
	 * @param ten_bac_dao_tao
	 * @return
	 * @throws Exception
	 */
	public ArrayList<BacDaoTaoM> searchBDT(String ten) throws Exception {
		ArrayList<BacDaoTaoM> list = getAll("ten LIKE '%" + ten + "%'");
		return list;
	}
	
	/**
	 * @function get all BacDaoTao with search + paging
	 * @param from
	 * @param to
	 * @return
	 * @throws Exception
	 */
	public ArrayList<BacDaoTaoM> getLimit(String ten, int from, int to) throws Exception {
		String limit = from + ", " + to;
		ArrayList<BacDaoTaoM> list = getAll("ten LIKE '%" + ten + "%'", limit);
		return list;
	}

	/**
	 * function insert BacDaoTao
	 * @param bdt
	 * @return
	 * @throws Exception
	 */
	public boolean Insert(BacDaoTaoM bdt) throws Exception {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("ten", bdt.getTen());
		return this.conn.Insert("BAC_DAO_TAO", map);
	}

	/**
	 * @function update BacDaoTao
	 * @param bdt
	 * @return
	 * @throws Exception
	 */
	public boolean Update(BacDaoTaoM bdt) throws Exception {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("ten", bdt.getTen());
		return this.conn.Update("BAC_DAO_TAO", map, "id_bac_dao_tao = " + bdt.getId_bac_dao_tao());
	}

	/**
	 * @function delete BacDaoTao
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public boolean Delete(int id) throws Exception {
		return this.conn.Delete("BAC_DAO_TAO", "id_bac_dao_tao = " + id);
	}
}